# -*- coding: utf-8 -*-
import scrapy

class Item(dict):
    class_name = ''

    def __init__(self, selector, class_name=''):
        super().__init__()
        self._selector = selector
        self.class_name = class_name

    def add_path(self, attr, xpath, begin=0, end=None):
        if end is None:
            self[attr] = self._selector.xpath(xpath).get(begin)
            return
        self[attr] = self._selector.xpath(xpath).getall()[begin:end + 1]
class SpiderTousuSpider(scrapy.Spider):
    name = 'spider_tousu'
    allowed_domains = ['tousu.16888.com']
    start_urls = ['https://tousu.16888.com/']
    # 车名  投诉类型 投诉详情
    def parse(self, response):
        url=response.xpath("//a[@class='next']/@href").extract()
        if len(url)>0:
            url=response.urljoin(url[0])
            yield scrapy.Request(url)


        for li in response.xpath('//div[@class="content"]/ul/li'):
            name=li.xpath('div//div[@class="tousu-text-list clearfix"]/span[3]/a/text()').extract()[0]
            brand=li.xpath('div//div[@class="tousu-text-list clearfix"]/span[2]/a/text()').extract()[0]
            place=li.xpath('div/div/p/font/text()').extract()[0]
            complaints=li.xpath('div//dd[@class="clearfix"]//span/a/text()').extract()
            for complaint in complaints:
                details = li.xpath('div//dd[@class="clearfix"]//font/a/text()').extract()
                for detail in details:
                    item = Item('')
                    item['brand']=brand
                    item['name'] = name
                    item['complaints'] = complaint
                    item['detail'] = detail
                    item['place']=place
                    print(item)
                    yield item


#
#








